package org.jetnuke.modules.reviews;

import org.apache.log4j.Logger;

import java.net.URLDecoder;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.ReviewsAdd;
import org.jetnuke.util.JetUtil;
import org.jetnuke.util.StringUtils;

public class SendReview implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(SendReview.class);

	String title;
	String sender_name;
	String mail;
	String text;

	int score;

	String url;

	String url_title;

	public void setScore(int score) {
		this.score = score;
	}

	public void setText(String text) {
		this.text = text;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setUrl_title(String url_title) {
		this.url_title = url_title;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo,
	 *      java.lang.String, int)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		ReviewsAdd rv = new ReviewsAdd();

		rv.setTEXT(StringUtils.filterText(URLDecoder.decode(text), false));
		rv.setTITLE(title);
		rv.setREVIEWER(sender_name);
		rv.setEMAIL(mail);
		rv.setURL(url);
		rv.setURL_TITLE(url_title);
		rv.setSCORE(score);
		rv.setDATE(new Date());
		DataAccessDriver.getInst().newReviewsAddDAO().addNew(rv);
		// rv.save();
		JetUtil.redirect(vc, response, request.getContextPath()
				+ "/do/reviews/sent");

		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return null;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public void setSender_name(String sender_name) {
		this.sender_name = sender_name;
	}

}
